package com.example.ceramics.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.ceramics.R;
import com.example.ceramics.activity.AddressActivity;
import com.example.ceramics.activity.LoginActivity;
import com.example.ceramics.activity.OrderActivity;
import com.example.ceramics.activity.SettingActivity;
import com.example.ceramics.activity.TaskActivity;
import com.example.ceramics.activity.UserActivity;
import com.example.ceramics.data.Order;
import com.example.ceramics.database.dao.IUserDao;
import com.example.ceramics.database.dao.UserDaoImpl;
import com.example.ceramics.entity.UserEntity;
import com.example.ceramics.util.StringUtils;
import com.example.ceramics.view.CircleTransform;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.io.Serializable;

import static com.example.ceramics.view.RoundRectImageView.getRoundBitmapByShader;

public class MineFragment extends BaseFragment {

    //    private ImageView ivTest;
    private LinearLayout order;

    private RelativeLayout address;
    private RelativeLayout user;
    private RelativeLayout task;
    private RelativeLayout setting;
    private TextView tvName;
    private ImageView ivAvatar;
    private Uri uri;
    private String uriString;

    private IUserDao userDao;

    public MineFragment() {
    }

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
//        ivTest = mRootView.findViewById(R.id.iv_test);
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.image1);
//        Bitmap outBitmap =getRoundBitmapByShader(bitmap, 380,90,10, 0);
//        ivTest.setImageBitmap(outBitmap);
        order = mRootView.findViewById(R.id.order);

        address = mRootView.findViewById(R.id.address);
        user = mRootView.findViewById(R.id.user);
        task = mRootView.findViewById(R.id.task);
        setting = mRootView.findViewById(R.id.setting);
        tvName = mRootView.findViewById(R.id.tv_name);
        ivAvatar = mRootView.findViewById(R.id.iv_avatar);
    }

    @Override
    protected void initData() {
        userDao = new UserDaoImpl(getActivity());

        initInfo();
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(getStringFromSp("account"))) {
                    showToast("请先登录！");
                    navigateTo(LoginActivity.class);
                    return;
                }
                navigateTo(OrderActivity.class);
            }
        });

        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(getStringFromSp("account"))) {
                    showToast("请先登录！");
                    navigateTo(LoginActivity.class);
                    return;
                }
                navigateTo(AddressActivity.class);
            }
        });

        user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(getStringFromSp("account"))) {
                    showToast("请先登录！");
                    navigateTo(LoginActivity.class);
                    return;
                }
                navigateToForResult(UserActivity.class, 1);
            }
        });

        task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(getStringFromSp("account"))) {
                    showToast("请先登录！");
                    navigateTo(LoginActivity.class);
                    return;
                }
                navigateTo(TaskActivity.class);
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(getStringFromSp("account"))) {
                    showToast("请先登录！");
                    navigateTo(LoginActivity.class);
                    return;
                }
                navigateTo(SettingActivity.class);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            initInfo();
        }
    }

    private void initInfo() {
        if (StringUtils.isEmpty(getStringFromSp("account"))) {
            tvName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navigateTo(LoginActivity.class);
                }
            });
        } else {
            UserEntity user = userDao.getUser(getStringFromSp("account"));
            String name = user.getNickname();
            if (!StringUtils.isEmpty(name)) {
                tvName.setText(name);
            } else
                tvName.setText("用户"+user.getAccount());
//            ivAvatar.setImageResource(user.getImg());
            uriString = user.getImg();
            if (!StringUtils.isEmpty(uriString)) {
                uri = Uri.parse(uriString);
                Picasso.with(getContext())
                        .load(uri)
                        .transform(new CircleTransform())
                        .placeholder(R.mipmap.logo)
                        .error(R.mipmap.logo)
                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                        .config(Bitmap.Config.RGB_565)
                        .into(ivAvatar);
            }

//            SharedPreferences spFile = getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
//            tvName.setText(spFile.getString("name", "喜羊羊"));
//
//            uriString = spFile.getString("name", "喜羊羊");
//            uri = Uri.parse(uriString);
//            if (!StringUtils.isEmpty(uriString)) {
//                Picasso.with(getContext())
//                        .load(Uri.parse(uriString))
//                        .transform(new CircleTransform())
//                        .placeholder(R.mipmap.image2)
//                        .error(R.mipmap.image3)
//                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
//                        .config(Bitmap.Config.RGB_565)
//                        .into(ivAvatar);
//                Glide.with(getContext()).load(uri).placeholder(R.mipmap.image2).error(R.mipmap.image3).into(ivAvatar);
//            }
        }
    }
}